import types from './types'
import lodash from 'lodash'

export default {
  [types.SET_EVENTS] (state, payload) {
    state.events = payload
  },
  [types.SET_EVENT] (state, payload) {
    state.event = payload
  },
  [types.ADD_EVENT] (state, payload) {
    state.events.items.push(payload)
    state.events.total ++

    state.eventList.push(payload)
  },
  [types.DELETE_EVENT] (state, id) {
    let index = lodash.findIndex(state.events.items, ['id', id])
    if (index >= 0) {
      state.events.items.splice(index, 1)
    }

    index = lodash.findIndex(state.eventList, ['id', id])
    if (index >= 0) {
      state.eventList.splice(index, 1)
    }
  },
  [types.SET_EVENT_LIST] (state, payload) {
    state.eventList = payload
  },
  [types.UPDATE_EVENT] (state, payload) {
    let event = lodash.find(state.eventList, ['id', payload.id])
    if (event) {
      event = payload
    } else {
      state.eventList.push(payload)
    }
  }
}
